package com.UI 
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.ColorTransform;
	import flash.utils.Timer;
	/**
	 * ...
	 * @author Imola
	 */
	public class Table extends MovieClip
	{
		private var table:Sprite;
		private var xCoord:int;
		private var yCoord:int;
		private var isRed:Boolean;
		private var timer:Timer;
		private var redTimer:Timer;
		private var score:uint;
		private var ct:ColorTransform;
		
		public function Table(xCoord:int, yCoord:int) 
		{
			this.isRed = new Boolean(true);
			this.xCoord = xCoord;
			this.yCoord = yCoord;
			table = new Sprite();
			init();
			generateRandomTime();
			ct = new ColorTransform();
		}
		
		private function init():void {
			table.graphics.beginFill(0xff0000);
			table.graphics.drawRect(this.xCoord, this.yCoord, 150, 50);
			addChild(table);
			var table2:Sprite = new Sprite();
			addChild(table2);
		}
		
		private function generateRandomTime():void{
			var randNum:Number;
			randNum = randomNumbers(2, 10);
			timer = new Timer(randNum*1000, 0);
			timer.addEventListener(TimerEvent.TIMER, randGen);
			timer.start();
		}
		
		private function randGen(e:TimerEvent):void  {
			timer.stop();
			timer.removeEventListener(TimerEvent.TIMER, randGen);
			transformToBlue();
			redTimer = new Timer(2000);
			redTimer.addEventListener(TimerEvent.TIMER, unsuccessfullClick);
			redTimer.start();
			this.addEventListener(MouseEvent.CLICK, tableClicked);
		}
				
		private function tableClicked(ev:Event):void {
			redTimer.stop();
			redTimer.removeEventListener(TimerEvent.TIMER, unsuccessfullClick);
			this.removeEventListener(MouseEvent.CLICK, tableClicked);
			score++;
			transformToRed();
			generateRandomTime();
		}
		
		private function unsuccessfullClick(e:TimerEvent):void {
			this.removeEventListener(MouseEvent.CLICK, tableClicked);
			redTimer.stop();
			redTimer.removeEventListener(TimerEvent.TIMER, unsuccessfullClick);
			transformToRed();
			generateRandomTime();
		}
		
		private function randomNumbers(min:Number, max:Number):Number {
			var Results:Number = Math.floor(Math.random() * max) + min;
			return Results;
		}
		
		private function transformToRed():void {
			ct.color = 0xff0000; //piros
			table.transform.colorTransform = ct;
		}
		
		private function transformToBlue():void {
			ct.color = 0x0000ff; //kek
			table.transform.colorTransform = ct;
		}
	}

}